import wx
import os
import json
import threading

class jsonConfig(object):
    def saveConf(self, data):
        # 写入 JSON 数据
        path = os.getcwd()
        fileName = os.path.join(path, 'config.json')
        tmpData = json.dumps(data,indent=1)
        with open(fileName, 'w', newline='\n') as f:
            f.write(tmpData)

    def getConf(self):
        # 读取数据
        path = os.getcwd()
        fileName = os.path.join(path, 'config.json')
        with open(fileName, 'r') as f:
            return json.load(f)

class myInfoUi(wx.Frame,jsonConfig):
    def __init__(self, parent=None, title="MyTest",pos = (1000,680),size = (1024,600)):
        wx.Frame.__init__(self,parent=parent, title=title,size=size)
        self.panel = wx.Panel(self,size = (1024,600))

    def setUpStatusBar(self):
        self.uiStatusBar = {
            'Gauge':'',
            'Text1':'',
            'Text2':'',
            'BoxSizer':''
        }

        self.uiStatusBar['BoxSizer']=wx.BoxSizer(wx.HORIZONTAL)
        box1_label=wx.StaticBox(self.panel, -1, '')
        text1_box=wx.StaticBoxSizer(box1_label,wx.HORIZONTAL)
        box2_label=wx.StaticBox(self.panel, -1, '')
        text2_box=wx.StaticBoxSizer(box2_label,wx.HORIZONTAL)

        #进度条
        self.uiStatusBar['Gauge']=wx.Gauge(self.panel, -1, 100, size=(242,22), style = wx.GA_HORIZONTAL|wx.GA_TEXT)
        #静态文本1
        self.uiStatusBar['Text1']=wx.StaticText(self.panel,label = "None",size=(-1,22))
        text1_box.Add(self.uiStatusBar['Text1'])
        #静态文本2
        self.uiStatusBar['Text2']=wx.StaticText(self.panel,label = "otaIdle",size=(100,22))
        text2_box.Add(self.uiStatusBar['Text2'],proportion = 1)
        text2_box.Add(self.uiStatusBar['Gauge'],proportion = 0)

        self.uiStatusBar['BoxSizer'].Add(text1_box,proportion = 1,flag = wx.RIGHT,border = 3)
        self.uiStatusBar['BoxSizer'].Add(text2_box,proportion = 0)

    def statusGaugeShow(self):
        if(self.uiStatusBar['Gauge'] != ''):
            self.uiStatusBar['Gauge'].Show()
            self.panel.Layout()

    def statusGaugeHide(self):
        if(self.uiStatusBar['Gauge'] != ''):
            self.uiStatusBar['Gauge'].Hide()
        #self.panel.Layout()

    def statusGaugeSetValue(self,value):
        if(self.uiStatusBar['Gauge'] != ''):
            self.uiStatusBar['Gauge'].SetValue(value)

    def statusGaugeGetValue(self):
        if(self.uiStatusBar['Gauge'] != ''):
            return self.uiStatusBar['Gauge'].GetValue()
        else:
            return None

    def statusTextSet(self,num,content):
        if(self.uiStatusBar['Text'+str(num)] != ''):
            self.uiStatusBar['Text'+str(num)].SetLabel(content)

    def statusTextGet(self,num):
        if(self.uiStatusBar['Text'+str(num)] != ''):
            return self.uiStatusBar['Text'+str(num)].GetValue()

    def __createConfigMember(self):
        self.tmpConf = {
            "name": "OTA test soft",
            "conf": {
                "ftpConf": {
                    "host": "",
                    "user": "",
                    "password": "",
                    "localPath": "",
                    "remotePath": ""
                },
                "sftpConf": {
                    "host": "",
                    "user": "",
                    "password": "",
                    "localPath": "",
                    "remotePath": ""
                },
                "statusTimeOut": {
                    "otaCheckSim": "0",
                    "otaCheckNet": "0",
                    "otaCmdSend": "0",
                    "otaDownloading": "0",
                    "otaUpdateing": "0",
                    "otaSuccess": "0"
                },
                "adiffPkgName":{
                    "up": "old2new.bin",
                    "down": "new2old.bin"
                },
                "otaCmdConf":{
                    "httpRPath": "/"
                }
            }
        }
        self.netConfig=[
            {
                "host": "",
                "user": "",
                "password": "",
                "localPath": "",
                "remotePath": ""
            },
            {
                "host": "",
                "user": "",
                "password": "",
                "localPath": "",
                "remotePath": ""
            },
        ]
        self.stateMacConf = {
            "otaCheckSim": "6",
            "otaCheckNet": "6",
            "otaCmdSend": "10",
            "otaDownloading": "60",
            "otaUpdateing": "300",
            "otaSuccess": "10"
        }
        self.adiffConfig = {        #not use Now
            "upFile": "old2new.bin",
            "downFile": "new2old.bin",
        }
        self.otaCmdConf = {
            "httpRPath": "/"
        }

    def setUpInfoUi(self):
        self.__createConfigMember()
        self.uiDebugLevel = 0
        self.otaUiStatusTable = {
            'ftp-up':'ftp-down',
            'ftp-down':'',
            'http-up':'http-down',
            'http-down':''
        }
        self.otaConfig = {
            'testMode':'',
            'netMode':'ftp',
            'curStatus':''
        }
        self.infoUiCtr = {
            'InfoText':'',
            'BoxSizer':'',
        }
        sInfo_box = wx.StaticBox(self.panel, -1, "Info")
        self.infoUiCtr['BoxSizer'] = wx.StaticBoxSizer(sInfo_box,wx.VERTICAL)
        self.infoUiCtr['InfoText'] = wx.TextCtrl(self.panel,style = wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY)

        self.infoUiCtr['BoxSizer'].Add(self.infoUiCtr['InfoText'],proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3)

    def debugToUi(self,dataInfo):
        self.infoUiCtr['InfoText'].AppendText(dataInfo)

    def errorDebug(self,dataInfo):
        if(self.uiDebugLevel == 0):
            self.infoUiCtr['InfoText'].AppendText("ERROR:"+dataInfo)

    def warningDebug(self,dataInfo):
        if(self.uiDebugLevel == 0 or self.uiDebugLevel == 1):
            self.infoUiCtr['InfoText'].AppendText("WARNING:"+dataInfo)

#infoUi demo
if __name__ == '__main__':
    app = wx.App()
    frame = myInfoUi(title="While there is life,there is hope.")
    frame.setUpInfoUi()
    frame.setUpStatusBar()
    frame.infoUiCtr['BoxSizer'].Add(frame.uiStatusBar['BoxSizer'],proportion = 0,flag = wx.EXPAND)
    frame.panel.SetSizer(frame.infoUiCtr['BoxSizer'])
    frame.Show()
    frame.statusGaugeHide()
    app.MainLoop()

